package com.aaie.gru.position.controller;


import com.aaie.gru.login.entity.LoginUser;
import com.aaie.gru.position.entity.GruSysPosition;
import com.aaie.gru.position.entity.GruSysPositionGet;
import com.aaie.gru.position.service.IGruSysPositionGetService;
import com.aaie.gru.position.service.IGruSysPositionService;
import com.aaie.gru.response.ResMessage;
import com.aaie.gru.util.CommonUtil;
import com.aaie.gru.util.ExceptionEnums;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 登录用户位置 前端控制器
 * </p>
 *
 * @author lvxing
 * @since 2019-08-19
 */
@Api(tags = "登录用户位置模块")
@RestController
@RequestMapping("/position")
public class GruSysPositionController {

    @Autowired
    IGruSysPositionService gruSysPositionService;

    @Autowired
    IGruSysPositionGetService gruSysPositionGetService;

    /**
     * 获取特定用户的地理位置
     * @param userId 特定用户的用户id
     * @return
     */
    @ApiOperation(value = "获取特定用户的地理位置", notes = "获取特定用户的地理位置")
    @GetMapping("/getPositionByUserId")
    public String getPositionByUserId(String userId) {
        QueryWrapper<GruSysPosition> queryWrapper = new QueryWrapper<>();
        // 设置这个查询条件的思想是主键id就是用户id,不管查询还是插入更新都方便
        queryWrapper.eq("id", userId);
        GruSysPosition gruSysPosition = gruSysPositionService.getOne(queryWrapper);
        return ResMessage.ok(gruSysPosition);
    }

    @ApiOperation(value = "保存用户的地理位置", notes = "保存用户的地理位置")
    @PostMapping("/savePosition")
    public String savePosition(@RequestBody GruSysPosition gruSysPosition, HttpSession session) {
        LoginUser user = CommonUtil.getUser(session);
        if (user == null) {
            return ResMessage.error(ExceptionEnums.LOGINUSER_IS_NULL.getEmsg(), ExceptionEnums.LOGINUSER_IS_NULL.getEcode());
        }
        String userId = user.getUserId();
        gruSysPosition.setId(userId);
        gruSysPosition.setUserId(userId);
        String updateTime = CommonUtil.generateLocalDateTime();
        long updateTimeSort = CommonUtil.generateCurrentTimeMillis();
        String userName = CommonUtil.getUser(session).getUserName();
        gruSysPosition.setCreateName(userName);
        gruSysPosition.setCreateTime(updateTime);
        gruSysPosition.setCreateTimeSort(updateTimeSort);
        gruSysPosition.setUpdateName(userName);
        gruSysPosition.setUpdateTime(updateTime);
        gruSysPosition.setUpdateTimeSort(updateTimeSort);
        // 更新地理位置信息
        boolean b = gruSysPositionService.saveOrUpdate(gruSysPosition);
        // 更新获取地理位置信息的状态
//        QueryWrapper<GruSysPositionGet> queryWrapper = new QueryWrapper<>();
//        queryWrapper.like("target_id", userId);
//        List<GruSysPositionGet> list = gruSysPositionGetService.list(queryWrapper);
//        if (!CollectionUtils.isEmpty(list)) {
//            for (GruSysPositionGet gruSysPositionGet : list) {
//                gruSysPositionGet.setPositionGetStatus("2");
//            }
//            gruSysPositionGetService.saveOrUpdateBatch(list);
//        }
        if (b) {
            return ResMessage.ok(gruSysPosition);
        }
        return ResMessage.error("更新用户位置信息失败", 200);
    }
}
